package com.chookss.mine.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.mine.personal.PersonalHomeActivity;
import com.chookss.mine.entity.PraiseEntity;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.Utils;
import com.chookss.video.VideoCommentsInputDialog;
import com.chookss.view.CircleImageView;
import com.chookss.view.listener.CommentInputDialogListener;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.Nullable;

public class MyPraiseAdapter extends BaseQuickAdapter<PraiseEntity, BaseViewHolderEx> {
    private Context context;
    private int messageType;

    public MyPraiseAdapter(int layoutResId, Context context, @Nullable List<PraiseEntity> data, int messageType) {
        super(layoutResId, data);
        this.context = context;
        this.messageType = messageType;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, PraiseEntity item) {
        CircleImageView ivHead = helper.getView(R.id.ivHead);
        TextView tvName = helper.getView(R.id.tvName);
        TextView tvContent = helper.getView(R.id.tvContent);
        TextView tvTime = helper.getView(R.id.tvTime);
        TextView tvFocus = helper.getView(R.id.tvFocus);
        ImageView ivMessage = helper.getView(R.id.ivMessage);
        tvName.setText(item.getEmployeeName());

        GlideUtils.loadAvatar(context, item.getHeadPhotoThumbUrl(), ivHead);
        //isYoufans你是否关注了（0-否 1-是）点赞关注时候有值
        //youIsfans是否关注了你（0-否 1-是）点赞关注时候有值

        if (3 == messageType) {
            tvContent.setText(item.getMessageContent());
            tvTime.setText(Utils.getAccurateDate(item.getCreateTime()));
            ivMessage.setVisibility(View.VISIBLE);
            tvFocus.setVisibility(View.GONE);
            if (Utils.isNull(item.getImageUrl())) {
                Glide.with(context).load(R.drawable.default_news).into(ivMessage);
            } else {
                GlideUtils.load(context, item.getImageUrl(), ivMessage, R.drawable.default_news);
            }

        } else {
            //1：粉丝关注通知，2：点赞通知，3：评论通知，5关注
            if (messageType == 1) {
                tvContent.setText(item.getMessageTitle());
                tvTime.setText(Utils.getAccurateDate(item.getCreateTime()));
            } else if (5 == messageType) {
                tvContent.setText("关注了他");
                tvTime.setText(Utils.getAccurateDate(item.getConcernTime()));
            } else {
                tvContent.setText(item.getMessageContent());
                tvTime.setText(Utils.getAccurateDate(item.getCreateTime()));
            }

            ivMessage.setVisibility(View.GONE);
            tvFocus.setVisibility(View.VISIBLE);
            if ("1".equals(item.getYouIsfans())) {
                tvFocus.setText("已关注");
                if ("1".equals(item.getIsYoufans())) {
                    tvFocus.setText("互相关注");
                    tvFocus.setBackgroundResource(R.drawable.shape_white_cccccc_12);
                } else {
                    tvFocus.setBackgroundResource(R.drawable.shape_white_cccccc_12);
                }
                tvFocus.setTextColor(Color.parseColor("#333333"));
            } else {
                tvFocus.setText("关注");
                tvFocus.setBackgroundResource(R.drawable.shape_white_main_color_12);
                tvFocus.setTextColor(context.getResources().getColor(R.color.main_color));
            }
        }
        if (3 == messageType) {
            //评论右边是图片
            if (tvTime.getText().toString().contains("-")) {
                tvContent.setMaxWidth(Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 128) - Utils.dip2px(context, 8) * tvTime.getText().toString().length());
            } else {
                tvContent.setMaxWidth(Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 128) - Utils.dip2px(context, 10) * tvTime.getText().toString().length());
            }
        } else {
            if (tvTime.getText().toString().contains("-")) {
                tvContent.setMaxWidth(Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 164) - Utils.dip2px(context, 8) * tvTime.getText().toString().length());
            } else {
                tvContent.setMaxWidth(Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 164) - Utils.dip2px(context, 10) * tvTime.getText().toString().length());
            }
        }

        ivHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PersonalHomeActivity.class);

                if (5 == messageType) {
                    intent.putExtra("employeeCode", item.getEmployeeCode());
                } else {
                    intent.putExtra("employeeCode", item.getFromEmployeeCode());
                }
                context.startActivity(intent);
            }
        });
        tvFocus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                if ("1".equals(item.getYouIsfans())) {
                    toFollow("0", item, helper.getItemPosition());
                } else {
                    toFollow("1", item, helper.getItemPosition());

                }
            }
        });
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                if (3 == messageType) {
                    showCommentDialog(item);
                }
            }
        });
    }

    private void showCommentDialog(PraiseEntity praiseEntity) {
        VideoCommentsInputDialog dialog = new VideoCommentsInputDialog(context, "3");
        dialog.setListener(new CommentInputDialogListener() {
            @Override
            public void onClick(String data) {
                toComment(data, praiseEntity);
            }
        });
        dialog.show();
    }

    private void toFollow(String followStatus, PraiseEntity praiseEntity, int position) {
        Map<String, String> map = new HashMap<>();

        if (5 == messageType) {
            map.put("employeeCode", praiseEntity.getEmployeeCode());
        } else {
            map.put("employeeCode", praiseEntity.getFromEmployeeCode());
        }
        map.put("followStatus", followStatus);
//        状态 0取消 1 关注
        MyHttpRequest.getDefault().postRequestCompat(null, Urls.toFollow, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) throws Exception {
                JSONObject reObject = new JSONObject(data);
                if ("200".equals(reObject.getString("code"))) {
                    JSONObject result_object = reObject.getJSONObject("data");
                    if ("true".equals(result_object.getString("rst"))) {
                        if ("1".equals(followStatus)) {
                            MyToast.show("关注成功");
                        } else {
                            MyToast.show("取消关注");
                        }
                        if (5 == messageType) {
                            if ("1".equals(followStatus)) {
                                praiseEntity.setYouIsfans("1");
                            } else {
                                praiseEntity.setYouIsfans("0");
                            }
                            notifyItemChanged(position);
                        } else {
                            for (int i = 0; i < getData().size(); i++) {
                                if (praiseEntity.getFromEmployeeCode().equals(getData().get(i).getFromEmployeeCode())) {
                                    if ("1".equals(followStatus)) {
                                        getData().get(i).setYouIsfans("1");
                                    } else {
                                        getData().get(i).setYouIsfans("0");
                                    }
                                    notifyItemChanged(i);
                                }
                            }
                        }

                    } else {
                        MyToast.show(reObject.getString("msg"));
                    }
                } else {
                    MyToast.show(reObject.getString("msg"));
                }

            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    private void toComment(String comment, PraiseEntity praiseEntity) {
        Map<String, String> map = new HashMap<>();
        map.put("videoComment", comment);

        map.put("messageTitle", praiseEntity.getMessageContent());
        map.put("employeeCode", praiseEntity.getFromEmployeeCode());
        map.put("videoCoverPath", praiseEntity.getImageUrl());
        if (Utils.isNull(praiseEntity.getParentCode())) {
            map.put("lvlNo", "2");
            map.put("commentId", praiseEntity.getMessageTypeCode());
            map.put("parentCode", praiseEntity.getMessageTypeCode());
        } else {
            map.put("lvlNo", "3");
            map.put("commentId", praiseEntity.getParentCode());
            map.put("parentCode", praiseEntity.getParentCode());
        }
        map.put("commentType", "2");
//        状态 0取消 1 关注
        MyHttpRequest.getDefault().postRequestCompat(null, Urls.commentVideo, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) throws Exception {
                JSONObject reObject = new JSONObject(data);
                if ("200".equals(reObject.getString("code"))) {
                    MyToast.show("回复成功");
                } else {
                    MyToast.show(reObject.getString("msg"));
                }

            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
}
